const api = require("../../../utils/apiUtils");
const app = getApp();

Page({
  data: {
    avatarUrl:null,
    upUrl:'',
    nickName:null
  },
  //转base64
  _toBase64OFPng(url){
    return new Promise((resolve)=>{
        my.getFileSystemManager().readFile({
            filePath:url,
            encoding:'base64',
            success(res){
                resolve('data:image/jpeg;base64,'+res.data);
            },
            fail(err){
                console.log(err.errMsg)
            }
        })
    })
  },

  //选择头像
  onChooseAvatar(e){
      console.log(e);
      const { avatarUrl } = e.detail;
      this.setData({avatarUrl})
      const self = this;
      self._toBase64OFPng(avatarUrl).then(base64=>{
          api.post(api.url.userImgUp,{
              content:base64
          },res=>{
              console.log(res);
              if(res.data.code==200){
                  wx.showToast({
                    title: '上传头像成功',
                  })
                  self.setData({
                      upUrl:res.data.body.imgPath
                  })
              }else{
                  wx.showToast({
                      title: '上传头像失败',
                      icon:'error'
                  })
                  // this.setData({avatarUrl:null});
              }
          })
      })
  },

  //输入
  inputNick(e){
      console.log(e)
      this.setData({
          nickName:e.detail.value
      })
  },

  btnSubmit(){
      if(!this.data.upUrl && !this.data.nickName){
          my.showModal({
            title: '温馨提示',
            content: '未更新头像或昵称',
            showCancel:false,
          })
          return;
      }
      const self = this;
      api.post(api.url.userInfoUp,{
          nickname:self.data.nickName,
          faceImg:self.data.upUrl
      },res=>{
          console.log(res)
          if(res.data.code==200){
              my.showToast({
                  content: '更新成功！',
                  type:'success',
                  success(){
                      app.globalData.userInfo = '';
                      setTimeout(()=>{
                          my.reLaunch({
                              url: '/pages/index/index',
                          })
                      },800)
                  }
              })
          }else if(res.data.msg){
              my.showModal({
                content: res.data.msg,
                showCancel:false
              })
          }else{
              my.showToast({
                content: '保存失败',
                type:'error',
                mask:true
              })
          }
      })
  },

  onLoad() {
    let info = app.globalData.userInfo;
      if(info && info.loginToken){
          let img = null;
          if(info.memberFaceImg){
              let array = info.memberFaceImg.split('res.judaozhineng.com');
              if(array && array.length == 2){
                  img = array[1];
              }
          }
          this.setData({
              avatarUrl:info.memberFaceImg,
              upUrl:img?img:'',
              nickName:info.memberName
          })
      }else{
          my.showToast({
            content: '登陆失效',
            type:'error',
            mask:true
          })
          setTimeout(function(){
              my.redirectTo({
                  url:'/pages/index/index'
              });
          },700);
      }
  },
});
